#include "dataconfig.h"
#include "mycoin.h"
#include "mypushbutton.h"
#include "playscene.h"
#include <QDebug>
#include <QAction>
#include <QMenuBar>
#include <QMenu>
#include <QPainter>
#include <QPixmap>
#include <QLabel>
#include <QTimer>
#include <QMessageBox>
#include <QPropertyAnimation>
#include <QSoundEffect>

PlayScene::PlayScene(int levelNum)
{
    QString str = QString("第 %1 关").arg(levelNum);
    qDebug() << "进入了" << str;
    this->levelIndex = levelNum;

    // 初始化游戏场景
    this->setFixedSize(320,588);
    this->setWindowIcon(QPixmap(":/res/Coin0001.png"));
    this->setWindowTitle(str);

    // 创建菜单栏
    QMenuBar *mbar = menuBar();
    setMenuBar(mbar);

    //移除状态栏
    setStatusBar(nullptr);

    //创建开始菜单
    QMenu *startMenu = mbar->addMenu("开始");
    QAction *quitAction = startMenu->addAction("退出");
    connect(quitAction,&QAction::triggered,this,[=](){
        this->close();
    });

    // 返回按钮音效
    QSoundEffect *backSound = new QSoundEffect(this);
    backSound->setSource(QUrl::fromLocalFile(":/res/TapButtonSound.wav"));

    // 返回按钮
    MyPushButton *backBtn = new MyPushButton(":/res/BackButton.png",":/res/BackButtonSelected.png");
    backBtn->setParent(this);
    backBtn->move(this->width() - backBtn->width(),this->height() - backBtn->height() - 15);

    connect(backBtn,&MyPushButton::clicked,this,[=](){
        backSound->play();
        // 发送返回信号
        emit this->chooseBack();
    });

    // 关卡标识
    QLabel *label = new QLabel();
    label->setParent(this);
    QFont font;
    font.setFamily("华文新魏");
    font.setPointSize(20);
    label->setFont(font);

    label->setText(QString("Level: %1").arg(levelNum));
    label->setGeometry(30,this->height()-70,120,50);

    // 初始化关卡信息
    DataConfig config;
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++){
            gameArr[i][j] = config.mData[levelNum][i][j];
        }
    }

    // 绘制胜利图片
    QLabel * doneLabel = new QLabel;
    doneLabel->setParent(this);
    QPixmap donePix;
    donePix.load(":/res/LevelCompletedDialogBg.png");
    doneLabel->setGeometry(0,0,donePix.width(),donePix.height());
    doneLabel->setPixmap(donePix);
    // 游戏未完成放在窗口上方
    doneLabel->move((this->width() - donePix.width()) * 0.5,-donePix.height());

    // 完成时的背景音乐
    QSoundEffect *doneSound = new QSoundEffect(this);
    doneSound->setSource(QUrl::fromLocalFile(":/res/LevelWinSound.wav"));
    doneSound->setLoopCount(QSoundEffect::Infinite);

    // 翻硬币时的背景音乐
    QSoundEffect *changeSound = new QSoundEffect(this);
    changeSound->setSource(QUrl::fromLocalFile(":/res/ConFlipSound.wav"));

    // 绘制金币背景图
    QPixmap pix = QPixmap(":/res/BoardNode.png");
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++){
            QLabel *label = new QLabel();
            label->setGeometry(0,0,pix.width(),pix.height());
            label->setPixmap(pix);
            label->setParent(this);
            label->move(57+i*pix.width(),200+j*pix.height());

            //创建金币
            QString btnImg;
            if(gameArr[i][j]){
                btnImg = ":/res/Coin0001.png";
            }else {
                btnImg = ":/res/Coin0008.png";
            }
            MyCoin *coin = new MyCoin(btnImg);
            coin->setParent(this);
            coin->move(59+i*pix.width(),204+j*pix.height());
            coin->columnIndex = i;
            coin->rowIndex = j;
            coin->flag = this->gameArr[i][j] == 1; // 1 正面 0 反面
            // 保存金币实例到金币数组
            coinArr[i][j] = coin;
            // 点击金币后翻转
            connect(coin,&MyCoin::clicked,this,[=](){
                if(this->done) return;
                changeSound->play();
                qDebug() << QString("点击了金币位置: %1 %2").arg(coin->columnIndex).arg(coin->rowIndex);
                coin->changeCoin();
                this->gameArr[i][j] = this->gameArr[i][j] == 0 ? 1 : 0;

                // 延时翻转周围的金币
                QTimer::singleShot(300,this,[=](){
                    if(coin->rowIndex - 1 >= 0){ // 上面金币翻转
                        coinArr[coin->columnIndex][coin->rowIndex - 1]->changeCoin();
                        this->gameArr[coin->columnIndex][coin->rowIndex - 1] = this->gameArr[coin->columnIndex][coin->rowIndex - 1] == 0 ? 1 : 0;
                    }
                    if(coin->rowIndex + 1 < 4){ // 下边金币翻转
                        coinArr[coin->columnIndex][coin->rowIndex + 1]->changeCoin();
                        this->gameArr[coin->columnIndex][coin->rowIndex + 1] = this->gameArr[coin->columnIndex][coin->rowIndex + 1] == 0 ? 1 : 0;
                    }
                    if(coin->columnIndex - 1 >= 0){ // 左侧金币翻转
                        coinArr[coin->columnIndex - 1][coin->rowIndex]->changeCoin();
                        this->gameArr[coin->columnIndex - 1][coin->rowIndex] = this->gameArr[coin->columnIndex - 1][coin->rowIndex] == 0 ? 1 : 0;
                    }
                    if(coin->columnIndex + 1 < 4){ // 右侧金币翻转
                        coinArr[coin->columnIndex + 1][coin->rowIndex]->changeCoin();
                        this->gameArr[coin->columnIndex + 1][coin->rowIndex] = this->gameArr[coin->columnIndex + 1][coin->rowIndex] == 0 ? 1 : 0;
                    }
                    // 判断是否完成游戏
                    bool testDone = true;
                    for (int i = 0; i < 4; i++) {
                        for (int j = 0; j < 4; j++){
                            if(coinArr[i][j]->flag == false){
                                testDone = false;
                                break; // 退出内层循环
                            }
                        }
                        if(!testDone) break; // 退出外层循环
                    }
                    if(testDone){
                        for (int i = 0; i < 4; i++) {
                            for (int j = 0; j < 4; j++){
                                coinArr[i][j]->done = true;
                            }
                        }
                        // 将完成标识放到窗口展示
                        QPropertyAnimation *ani = new QPropertyAnimation(doneLabel,"geometry");
                        ani->setDuration(1000);
                        ani->setStartValue(QRect(doneLabel->x(),doneLabel->y(),doneLabel->width(),doneLabel->height()));
                        ani->setEndValue(QRect(doneLabel->x(),doneLabel->y()+114,doneLabel->width(),doneLabel->height()));
                        ani->setEasingCurve(QEasingCurve::OutBounce);
                        ani->start();
                        doneSound->play();
                        qDebug() << "恭喜你,完成闯关";
                        this->done = true;
                    }

                });
            });
        }
    }



}

void PlayScene::paintEvent(QPaintEvent *){
    QPainter painter(this);
    QPixmap pix;
    pix.load(":/res/PlayLevelSceneBg.png");
    painter.drawPixmap(0,0,pix.width(),pix.height(),pix);

    pix.load(":/res/Title.png");
    pix.scaled(pix.width()*0.5,pix.height()*0.5);
    painter.drawPixmap(20,30,pix.width(),pix.height(),pix);
}
